package com.norteksoft.projectname.module1.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.norteksoft.projectname.module1.model.Department;
import com.norteksoft.projectname.module1.service.DepartmentManager;
import com.norteksoft.struts.Constants;
import com.norteksoft.struts.dao.support.Page;
import com.norteksoft.struts.web.StrutsAction;
import com.norteksoft.struts.web.ec.ECInfomationBean;
import com.norteksoft.struts.web.ec.PaginationUtil;



/**
 * @spring.bean name="/actions/departmentDispatcher" singleton="false"
 * @author huhc
 *
 */
public class DepartmentDispatcher extends StrutsAction {
	private static Log log=LogFactory.getLog(DepartmentDispatcher.class);
	private DepartmentManager departmentManager;
	/**
	 * @spring.property ref="departmentManager"
	 * @param departmentManager
	 */
	public void setDepartmentManager(DepartmentManager departmentManager) {
		this.departmentManager = departmentManager;
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.findForward(Constants.ADD);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		Department department=new Department();
		this.bindEntity(form,department);
		departmentManager.save(department);
		StringBuffer url=new StringBuffer("/actions/departmentDispatcher.do?method=list");
		refreshOpener(url,request, response);
		return null;
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long id=Long.valueOf(request.getParameter("id"));
		Department department=(Department) departmentManager.get(id);
		this.bindForm(form, department);
		//request.setAttribute("department", department);
		return mapping.findForward(Constants.EDIT);
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long id=Long.valueOf(request.getParameter("id"));
		Department department=(Department) departmentManager.get(id);
		this.bindForm(form, department);
		departmentManager.save(department);
		return mapping.findForward(Constants.RETURN_TO_lIST);
	}
	
	public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long id=Long.valueOf(request.getParameter("id"));
		Department department=(Department) departmentManager.get(id);
		//this.bindForm(form, department);
		request.setAttribute("department", department);
		return mapping.findForward(Constants.SHOW);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] ids=request.getParameterValues("docId");
		for(int i=0;i<ids.length;i++){
			Long id=Long.valueOf(ids[i]);
			departmentManager.removeById(id);
		}
		return mapping.findForward(Constants.RETURN_TO_lIST);
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//int totalRows=departmentManager.getCount();
		//Page page=new Page("from Department d");
		//page.prepare(totalRows, request);
		ECInfomationBean ecInfo = PaginationUtil.initializeEC(request);
		Page page=departmentManager.pagedQuery(getHql(ecInfo.getSortQuery()), ecInfo.getCurPage(), ecInfo.getPageSize(), null);
		//List departmentList=departmentManager.list(getHql(ecInfo.getSortQuery()), ecInfo.getCurPage(), ecInfo.getPageSize(),null);
		request.setAttribute("departmentList", page.getResult());
		//ec必须设置这个属性
		request.setAttribute("totalRows", new Integer(page.getTotalCount()));
		return mapping.findForward(Constants.LIST);
	}

	private String getHql(String sortQuery) {
		// TODO Auto-generated method stub
		StringBuffer sb=new StringBuffer("from Department d");
		sb.append(sortQuery);
		return sb.toString();
	}
	
	
}
